// packageD/newPage/uploadAuthorization/uploadAuthorization.js
var t = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    uploadShow: false,
    imgUrl: "", //授权信息图片
    idcardFront: "", //身份证正面
    idcardBack: "", //身份证反面
    email: "", //邮箱
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'options')
    this.setData({
      orderId: options.id,
      imgUrl: options.pingzheng
    })
  },
  downFile() {
    // window.open("https://shops.cg500.com/static/weituoshu.docx");
    //加载框动画
    wx.showLoading({
      title: '正在下载……'
    });
    wx.downloadFile({
      url: 'https://shops.cg500.com/static/weituoshu.docx', //下载地址
      success: (data) => {
        if (data.statusCode === 200) {
          //隐藏加载框
          wx.hideLoading();
          //文件保存到本地
          wx.getFileSystemManager().saveFile({
            tempFilePath: data.tempFilePath, //临时路径
            success: function (res) {
              wx.showToast({
                icon: 'success',
                mask: true,
                title: '下载成功', //保存路径
                duration: 2000,
              });

              //自动打开文档查看
              setTimeout(() => {
                var filePath = res.savedFilePath;
                wx.openDocument({ //新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
                  filePath: filePath,
                  showMenu: true,
                  success: function (res) {
                    console.log('打开文档成功');
                  }
                });
              }, 2000)
            }
          });
        }
      },
      fail: (err) => {
        uni.showToast({
          icon: 'none',
          mask: true,
          title: '文件下载失败',
        });
      },
    });

  },
  changeEmail(e) {
    console.log(e.detail.value, 'changeEmail')
    this.setData({
      email: e.detail.value
    })
  },
  onClose() {
    this.setData({
      uploadShow: false
    })
  },
  uploadFile() {
    // this.setData({
    //   uploadShow: true
    // })
    // return;

    var that = this;
    var o = t.getNetAddresss("TuWenhua.is_shiming");
    t._postNetWork({
      url: o,
      data: {
        uid: wx.getStorageSync('yz_uid')
      },
      success: function (i) {
        var o = i.data;
        console.log(o, 'ooooooo')
        that.setData({
          idcardFront: o.data.idcard_a, //身份证正面
          idcardBack: o.data.idcard_b, //身份证反面
          email: o.data.email, //邮箱
        })
        if (1 != o.result) return t.tips(o.msg);
        if (o.data.realname == "") {
          t.tips("必须实名认证");
          setTimeout(() => {
            wx.navigateTo({
              url: '/packageD/newPage/realAuth/realAuth?type=2' + '&orderId=' + that.data.orderId
            })
          }, 1000);
        } else {
          that.setData({
            uploadShow: true
          })
        }
      }
    });

  },
  choosePicture(e) {
    console.log(e.currentTarget.dataset.type, 'choosePicture')
    // return;
    let type = e.currentTarget.dataset.type
    var that = this;
    wx.chooseImage({
      count: 1, // 可选择图片的数量
      sizeType: ['original', 'compressed'], // 可选择图片的类型：原图或压缩图
      sourceType: ['album', 'camera'], // 可选择图片的来源：相册或相机
      success: (res) => {
        const tempFilePaths = res.tempFilePaths

        // 调用上传图片函数
        that.uploadImage(tempFilePaths[0], type)
      }
    })
  },
  // 上传图片
  uploadImage: function (imagePath, type) {
    let typeUpload = type;
    var that = this;
    var i = t.getNetAddresss("upload.uploadPic");
    wx.uploadFile({
      url: i, // 图片上传接口的URL
      filePath: imagePath,
      name: 'file', // 上传图片时的名称
      success: (res) => {
        // 图片上传成功后的处理逻辑
        console.log(JSON.parse(res.data).data.img_url, 'uploadImage')
        let image = JSON.parse(res.data).data
        // let imgUrl = JSON.parse(res.data).data.img_url;
        if (typeUpload == 0) {
          that.setData({
            imgUrl: image.img_url
          })
        } else if (typeUpload == 1) {
          that.setData({
            idcardFront: image.img_url
          }) //身份证正面
        } else if (typeUpload == 2) {
          that.setData({
            idcardBack: image.img_url
          }) //身份证反面
        }
        console.log(that.data.imgUrl, that.data.idcardFront, that.data.idcardBack, 'hahahahahahah')
        // that.setData({
        //   imagePath: tempFilePaths[0]
        // })
        // var o = t.getNetAddresss("TuWenhua.pingzheng");
        // t._postNetWork({
        //   url: o,
        //   data: {
        //     order_id: that.data.orderId,
        //     url: imgUrl
        //   },
        //   success: function (i) {
        //     var o = i.data;
        //     console.log(o, 'ooooooo')
        //     if (1 != o.result) return t.tips(o.msg);
        //     wx.redirectTo({
        //       url: '/packageA/member/myOrder_v2/myOrder_v2?status=2',
        //     })
        //   }
        // })
      },
      fail: (error) => {
        // 图片上传失败后的处理逻辑
        console.log(error)
      }
    })
  },
  submit() {
    var that = this;
    if (this.data.imgUrl == '') {
      wx.showToast({
        title: '授权信息不能为空',
        icon: 'none'
      })
      return;
    }
    if (this.data.email == '') {
      wx.showToast({
        title: '邮箱不能为空',
        icon: 'none'
      })
      return
    }
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (!re.test(this.data.email)) {
      wx.showToast({
        title: '请输入正确的邮箱',
        icon: 'none'
      })
      return
    }
    if (this.data.idcardFront == '') {
      wx.showToast({
        title: '身份证正面照不能为空',
        icon: 'none'
      })
      return
    }
    if (this.data.idcardBack == '') {
      wx.showToast({
        title: '身份证背面照不能为空',
        icon: 'none'
      })
      return
    }
    //  上传订单凭证
    var o = t.getNetAddresss("TuWenhua.pingzheng");
    t._postNetWork({
      url: o,
      data: {
        order_id: that.data.orderId,
        url: that.data.imgUrl,
        email: that.data.email,
        idcard_a: that.data.idcardFront,
        idcard_b: that.data.idcardBack,
        uid: wx.getStorageSync('yz_uid')
      },
      success: function (i) {
        var o = i.data;
        console.log(o, 'ooooooo')
        if (1 != o.result) return t.tips(o.msg);
        wx.redirectTo({
          url: '/packageA/member/myOrder_v2/myOrder_v2?status=2',
        })
      }
    })



    // $http
    //   .post("TuWenhua.pingzheng", {
    //     order_id: That.orderId,
    //     url: That.imgUrl,
    //     email: That.email,
    //     idcard_a: That.idcardFront,
    //     idcard_b: That.idcardBack,
    //     uid: localStorage.getItem("uid")? localStorage.getItem("uid") : localStorage.getItem("mid")
    //   })
    //   .then(res => {
    //     if (res.result == 0) {
    //       return this.$toast(res.msg);
    //     }
    //     this.$toast("上传成功");
    //     this.uploadShow = false;
    //     this.$router.push(this.fun.getUrl("orderlist", { status: "2" }));
    //   });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})